package com.xjm.api.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjm.entity.model.UsersModel;
import com.xjm.entity.vo.PageVo;
import com.xjm.entity.vo.Result;
import com.xjm.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/user")
@RequiredArgsConstructor
public class UsersController {

    private final UserService userService;

    @GetMapping
    public Result<PageVo<UsersModel>> list(
            @RequestParam(defaultValue = "1") Long page,
            @RequestParam(defaultValue = "5") Long size
    ) {
        return Result.success(userService.page(new Page<>(page, size)));
    }

    @GetMapping("/{id}")
    public Result<UsersModel> getById(@PathVariable Long id) {
        return Result.success(userService.getById(id));
    }

    @PostMapping
    public Result<UsersModel> add(@RequestBody UsersModel UsersModel) {
        userService.save(UsersModel);
        return Result.success();
    }

    @PutMapping
    public Result<UsersModel> update(@RequestBody UsersModel UsersModel) {
        userService.updateById(UsersModel);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<UsersModel> delete(@PathVariable Long id) {
        userService.removeById(id);
        return Result.success();
    }
}
